package com.example.seakdreamandroid.voiceCommunity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.whiteNoise.WhiteNoiseOfficialChannel;
import com.example.seakdreamandroid.whiteNoise.WhiteNoiseOfficialChannelAdapter;

import java.util.List;

public class VoiceRemarkAdpter extends BaseAdapter {
    //上下文环境
    private Context mContext;
    //数据源
    private List<VoiceRemark> mlist;

    public VoiceRemarkAdpter(Context mContext, List<VoiceRemark> mlist) {
        this.mContext = mContext;
        this.mlist = mlist;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    private static class ViewHolder{
        //每一个item中的控制
        ImageView head,remarkUp;
        TextView name,time,content,countUp;
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
       ViewHolder viewHolder;
       if(view == null){
           //加载item的布局文件，生成一个view对象，再给view赋值
           view = LayoutInflater.from(mContext).inflate(R.layout.item_voice_remark_listview,null);
            viewHolder = new ViewHolder();
            viewHolder.head = view.findViewById(R.id.voice_remark_content_head);
            viewHolder.remarkUp = view.findViewById(R.id.voice_remark_up_color);
            viewHolder.name = view.findViewById(R.id.voice_remark_content_name);
            viewHolder.time = view.findViewById(R.id.voice_remark_content_time);
            viewHolder.content = view.findViewById(R.id.voice_remark_content);
            viewHolder.countUp = view.findViewById(R.id.voice_remark_up_count);
           //将viewHolder对象同view关联起来
           view.setTag(viewHolder);
       }else {
           //重复利用view对象
           //获取到跟当前convertView绑定在一起的viewholder对象
           viewHolder = (ViewHolder) view.getTag();
       }
        VoiceRemark voiceRemark = mlist.get(position);
        byte[] bs = Base64.decode(voiceRemark.getHead(), Base64.DEFAULT);
        for (int i = 0; i < bs.length; ++i) {
            if (bs[i] < 0) {
                bs[i] += 256;
            }
        }
        Bitmap bitmap = BitmapFactory.decodeByteArray(bs,0,bs.length);

        Glide.with(mContext).load(bitmap).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(viewHolder.head);
//        RequestOptions requestOptions = new RequestOptions();
//        Glide.with(mContext)
//                .load(voiceRemark.getHead())
//                .into(viewHolder.head);
        viewHolder.remarkUp.setImageResource(voiceRemark.getRemarkUp());
        viewHolder.name.setText(voiceRemark.getName());
        viewHolder.time.setText(voiceRemark.getTime());
        viewHolder.content.setText(voiceRemark.getContent());
        viewHolder.countUp.setText(voiceRemark.getCountUp());
        return view;
    }
}
